

#ifndef __WIRELESS_H
#define __WIRELESS_H

#include "WirelessDriver.h"
#include "string.h"

#define Txms 5000
#define TSLOT 200
#define MAX_REPEAT_SEND 20

#define WIRELESS_RECEIVE_ENABLE 1
#define WIRELESS_RECEIVE_DISABLE 0

#define WIRELESS_BUFF_PACKAGE_LEN 64

#define COMPARE_STRING_LEN 20

#define TEMP_BUFF_LEN 20

#define WIRELESS_FREE_RSSI 55
#define WIRELESS_CSMA_TIME 1000

#define WIRELESS_UPLOAD_SWITCH_STATUS 0X03
#define WIRELESS_UPLOAD_QUENTITY 0X04
#define WIRELESS_UPLOAD_SWITCH_QUENTITY 0X05
#define WIRELESS_MATCH_REQUEST 0X06
#define WIRELESS_MASTER_REVISE_PARA 0X07
#define WIRELESS_CHECK_ONLINE 0X08
#define WIRELESS_ALLOW_REVISE_PARA 0X09
#define WIRELESS_NULL_ACK 0

#define WIRELESS_UPLOAD_PARAMETER_ASK_OK 0X02
#define WIRELESS_RECEIVE_PARAMETER_ACK_OK 0x01
#define WIRELESS_UPLOAD_PARAMETER_ACK_OK WIRELESS_UPLOAD_PARAMETER_ASK_OK
#define WIRELESS_SEND_WAIT_ACK_TIMEOUT 0X0B


#define WIRELESS_MATCH_CHANNEL 0
#define WIRELESS_MATCH_PANID 0X2DD4



#define WIRELESS_RECORD_HEAD1 0XAA
#define WIRELESS_RECORD_HEAD2 0X55

#define WIRELESS_TEMP_BUFF_LEN 20

#define WIRELESS_FRAME_HEAD1 0XAA
#define WIRELESS_FRAME_HEAD2 0X55
#define WIRELESS_FRAME_TAIL 0X16

#if 0

#define WIRELESS_REQUEST_JOIN_NET 0X09
#define WIRELESS_UPLOAD_PARAMETER 0X03
#define WIRELESS_REVISE_UPLOAD_TIME 0X0A
#define WIRELESS_RECEIVE_PARAMETER 0X06


#define WIRELESS_REQUEST_JOIN_NET_ACK_ERROR 0X89
#define WIRELESS_UPLOAD_PARAMETER_ACK_ERROR 0X83
#define WIRELESS_REVISE_UPLOAD_TIME_ACK_ERROR 0X8A

#define WIRELESS_REQUEST_JOIN_NET_ACK_OK WIRELESS_REQUEST_JOIN_NET
#define WIRELESS_UPLOAD_PARAMETER_ACK_OK WIRELESS_UPLOAD_PARAMETER
#define WIRELESS_REVISE_UPLOAD_TIME_CMD WIRELESS_REVISE_UPLOAD_TIME
#define WIRELESS_RECEIVE_PARAMETER_ACK_OK WIRELESS_RECEIVE_PARAMETER
#endif

typedef enum
{
	WIRELESS_SET_GET_LOCAL_ADDRESS = 0,
	WIRELESS_SET_CHECK_PANID,
	WIRELESS_SET_CHECK_PANID_ACK,
	WIRELESS_SET_CHECK_CHANNEL,
	WIRELESS_SET_CHECK_CHANNEL_ACK,
	WIRELESS_SET_TX_POWER,
	WIRELESS_SET_TX_POWER_ACK,
	WIRELESS_SET_CHECK_ADDRESS,
	WIRELESS_SET_CHECK_ADDRESS_ACK,
	WIRELESS_SET_WRITE_ADDRESS,
	WIRELESS_SET_WRITE_ADDRESS_ACK,
	WIRELESS_SET_RESET_MODULE,
	WIRELESS_SET_WAIT_MODULE_READY,
	WIRELESS_SET_SUCCESS,
	
	WIRELESS_SET_STEP_MAX
}EnumWirelessSetStep;


typedef enum
{
	COMPARE_STR1 = 0,
	COMPARE_STR2,
	COMPARE_STR_MAX
}EnumCompareStringNumbers;

typedef struct
{
	unsigned char CompareString[COMPARE_STRING_LEN];
	unsigned char CompareResults;
}StructCompareString;

extern StructCompareString  gsCompareString[COMPARE_STR_MAX];


typedef struct
{
	unsigned char SendPacakageBuff[WIRELESS_BUFF_PACKAGE_LEN];
	unsigned char SendValidLen;
	
	unsigned char RecePacakageBuff[WIRELESS_BUFF_PACKAGE_LEN];
	unsigned char RecePacakageBuffCopy[WIRELESS_BUFF_PACKAGE_LEN];
	unsigned char ReceiveValidLen;
	unsigned char ReceiveValidLenCopy;
	unsigned char ReceiveFrameFlag;
	unsigned char ReceiveAllowFlag;
}StructWirelessUartPacakage;

extern StructWirelessUartPacakage gWirelessUartPacakage;

typedef struct
{
	unsigned char LocalAddress;
	unsigned short int Panid;
	unsigned char Channel;//
	unsigned char TxPower;
	unsigned short int WakeupTime;
	unsigned char AllowSleepFlag;
	unsigned char RSSI;
	unsigned char SendFinishFlag;
}StructWirelessModuleParameter;

extern StructWirelessModuleParameter gWirelessModuleParameter;



typedef enum
{
       WIRELESS_ENTER_SET_MODE = 0,
       REVISE_ADDRESS,
       CHECK_ADDRESS_OK,
	REIVSE_PANID,
	CHECK_PANID_OK,
	REVISE_CHANNEL,
	CHECK_CHANNEL_OK,
	WIRELESS_RST,
	WIRELESS_RST_OK,
	ENTER_P2P_MODE,
	REQUEST_TO_CO_NET,
	WAIT_CO_ACK,
	MATCH_CODE_SUCCESS,
	MATCH_CODE_FAIL,
	MATCH_CODE_STEP_MAX
}EnumWirelessMatchCodeStep;
extern EnumWirelessMatchCodeStep gWirelessMatchCodeStep;

typedef enum
{
	W_LOGIC_RUN_INIT = 0,
	W_LOGIC_CHECK_EXIST,
	W_LOGIC_CHECK_EXIST_ACK,
	W_LOGIC_PARAMETER_INIT,
	W_LOGIC_REVISE_PARAMETER_SELF,
	W_LOGIC_SEND_REQUESET_TO_CO,
	W_LOGIC_WAIT_CO_ACK,
	W_LOGIC_WAIT_ALLOW_REVISE,
	W_LOGIC_REVISE_NEW_NET_PARA,
	W_LOGIC_REVISE_PARA_OK,
	W_LOGIC_TEST_NET_ENVIROMENT,
	W_LOGIC_TEST_NET_ENVIROMENT_CO_ACK,
	W_LOGIC_REVISE_COOR_PC,
	W_LOGIC_MATCH_END,
	W_LOGIC_RECOVER_DEFAULT_SELF,
	W_LOGIC_RUN_STEP_MAX
}EnumWirelessLogicRunStep;
extern EnumWirelessLogicRunStep gWirelessLogicRunStep;

typedef enum
{
       W_LOGIC_REVISE_INIT = 0,
	W_LOGIC_REIVSE_PARAMETER_SELF_OK ,
	W_LOGIC_REVISE_ADDRESS_OK,
	W_LOGIC_REVISE_PANID_OK,
	W_LOGIC_REVISE_CHANNEL_OK,
	W_LOGIC_ALLOW_JOIN_NET_OK,
	W_LOGIC_REVISE_SEND_DATA,
	W_LOGIC_ACK_REQUES_OK,
	W_LOGIC_ACK_REQUES_FAIL,
	W_LOGIC_MATCH_SUCCESS,
	W_LOGIC_ACK_OK,
	W_LOGIC_ERROR,
	W_LOGIC_STATUS_MAX
}EnumWirelessLogicRunStatus;


typedef struct
{
	unsigned short int Address;
	unsigned short int panid;
	unsigned long int channel;
	unsigned short int TimeoutCnt;
	EnumWirelessLogicRunStatus WlogicStatus;
}StructMatchParameter;

extern StructMatchParameter gsMatchParameter;

extern void StartTimer5(unsigned short int xus);
extern void StopTimer5(void);

extern volatile EnumWirelessSetStep gWirelessSetParaRunStep;

extern volatile unsigned char gWirelessCOAckType;


void WirelessModuleInit(void);

void WirelessSendDataByUartIT(void);
void WirelessReceiveDataByUartIT(unsigned char dat);
void WirelessReceiveAllowEnable(void);
void WirelessReceiveAllowDisable(void);

void SetAckCompareString(EnumCompareStringNumbers snum,const unsigned char *psas);
void ClearAckCompareString(void);
unsigned char CheckCompareResult(EnumCompareStringNumbers snum);
void ExecuteAckCompare(void);

void WirelessSendData(const unsigned char *sdata,unsigned char len);
void WirelessSendString(const unsigned char *sdata);

void WirelessTranslateCOAck(void);
unsigned char WirelessAnalysisProperty(unsigned short int pos);
unsigned char WirelessGetValue(unsigned char dat);
void GetLocalAddress(void);

void WirelessModuleParameterInit(void);

void WirelessSendPacakage(unsigned char AimAddress,
                                                    unsigned char FunctionCode,
                                                    const unsigned char *InBuffer,
                                                    unsigned char InDataLen);
void WiresslessRequestJoinNet(void);
void WirelessMatchCode(void);

void ReadWirelessParameter(void);
void SaveWirelessParameter(void);
void WirelessProtocolAnalysis(void);

void WirelessRecoveryOldValueInit(void);
unsigned char WirelessRecoveryOldValue(void);

unsigned short int WirelessGetModuleAddress(void);
unsigned long int WirelessGetModuleChannel(void);
unsigned short int WirelessGetModulePanid(void);

void WirelessRevisePANID_CHANNEL(void);

unsigned long int WirelessGetModuleInterParmeter(const char *str);
void WirelessSetModuleInterParameter(const char *scmd,unsigned long int dat);

void WirelessSaveMatchParameter(void);

void WiresslessAckReceiveParameter(void);

void DeleteAddress(void);

void WirelessDisposedInterruptEvents(void);

void WirelessSendCSMA_CA(void);

#endif

